package com.step.ems.component

import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import com.step.ems.R

@Composable
fun MyDialog(
    openDialog: Boolean = false,
    onFirst: () -> Unit = {},
    onSecond: () -> Unit = {},
    onDismissRequest: () -> Unit = {},
    title: String = "",
    content: String = "",
    @StringRes firstTextId: Int = R.string.dismiss,
    @StringRes secondTextId: Int = R.string.confirm,
) {
    if (openDialog) {
        MyDialogBody(
            onFirst = onFirst,
            onSecond = onSecond,
            onDismissRequest = onDismissRequest,
            title = title,
            content = content,
            firstTextId = firstTextId,
            secondTextId = secondTextId,
        )
    }
}

@Composable
private fun MyDialogBody(
    onFirst: () -> Unit = {},
    onSecond: () -> Unit = {},
    onDismissRequest: () -> Unit = {},
    title: String = "",
    content: String = "",
    @StringRes firstTextId: Int = R.string.dismiss,
    @StringRes secondTextId: Int = R.string.confirm,
) {
    Dialog(onDismissRequest = onDismissRequest) {
        Column(
            modifier = Modifier
                .padding(horizontal = 25.dp)
                .background(Color.White, shape = RoundedCornerShape(8.dp))
                .padding(start = 20.dp, end = 20.dp, top = 20.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Image(
                painter = painterResource(id = R.drawable.logo),
                contentDescription = "",
                contentScale = ContentScale.Fit,
                modifier = Modifier
                    .width(90.dp)
                    .height(35.dp)
            )
            if (title.isNotBlank()) {
                Text(
                    text = title,
                    modifier = Modifier.padding(top = 14.dp),
                    textAlign = TextAlign.Center,
                    color = Color.Black,
                    fontWeight = FontWeight.ExtraBold,
                    fontSize = 17.sp
                )
            }
            if (content.isNotBlank()) {
                Text(
                    text = content,
                    modifier = Modifier.padding(top = 6.dp),
                    textAlign = TextAlign.Center,
                    color = Color.Gray,
                    lineHeight = 18.sp,
                    fontSize = 14.sp
                )
            }
            Column(
                modifier = Modifier
                    .padding(top = 13.dp)
                    .fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Button(
                    modifier = Modifier
                        .fillMaxWidth(),
                    onClick = {
                        onFirst()
                    },
                    shape = RoundedCornerShape(4.dp)
                ) {
                    Text(stringResource(id = firstTextId))
                }
                TextButton(
                    modifier = Modifier.fillMaxWidth(),
                    onClick = {
                        onSecond()
                    },
                ) {
                    Text(stringResource(id = secondTextId))
                }
            }
        }
    }
}